<?php
//新闻
define('IN_T', true);
require 'include/init.php';

$act = Common::sfilter($_REQUEST["act"]);

if ($act == "ajaxNewsList") {
    //ajax获取新闻列表
    $cat_type = intval($_REQUEST['cat_type']);
    $page = intval($_REQUEST['page']);
    $page = $page < 1 ? 1 : $page;
    $size = 10;
    $list = get_article_list($cat_type, $page, $size);
    echo $Json->encode($list);
    exit;
}
//显示列表或详情
else {
	$c_id = 0;	//用于返回商户的全局商户id
    $nid = intval($_REQUEST['nid']); //新闻id
	
    //未传nid，则是列表
    if ($nid < 1) {
        $cat_type = intval($_REQUEST['cat_type']); //分类id
        //判断cat_type是否存在
        $news = get_article_list($cat_type,1,2);
        if (!empty($news) && !empty($news[0]['cname'])) {
            //提取cat_type的列表
            if (count($news) == 1) {
                //新闻列表数量为1，直接跳转到详情页
                Common::base_header("Location:/news/?nid=" . $news[0]['id']);
            } 
			else {
                //只提取第一个新闻的第一个图片
                foreach ($news as $k => $v) {
                    $pics = Common::get_pics_from_html($v['content']);
                    $news[$k]['pic'] = $pics[0];
                    break;
                }
                $tp->assign('act', "lists");
				$tp->assign('title', $news[0]['cname']);
				$tp->assign('c_id',$news[0]['c_id']);
                $tp->assign("cat_type", $cat_type);
            }
        }
    } 
	//nid>0,新闻详情
    else {
        //检验nid是否合法
        $sql = "SELECT news.* FROM " . $GLOBALS['Base']->table('company_news') . " as news " .
               "WHERE news.id=" . $nid . " ";
        $news = $Db->query($sql, "Row");        
		if(!empty($news)){
			$tp->assign('title', $news['title']);
			$tp->assign('nid', $nid);
			$tp->assign('c_id',$news['c_id']);
			//判断当前文章所在分类的文章数量
			$count = $Db->getCount($Base->table('company_news'),'id',array('cat_type'=>$news['cat_type']));
			$back_url = $count==1 ? '/company/?cid='.$news['c_id'] : '/news/?cat_type='.$news['cat_type'];
			$tp->assign('back_url',$back_url);
			//读取他的子文章
            $sql = "select * from ".$Base->table('company_news')." where c_id=".$news['c_id']." and new_type=".$nid;
            $childNews = $Db->query($sql);
            $tp->assign('childNews',$childNews);
		}
    }
}
$tp->assign('news', $news);
$tp->display('news.tpl');

function get_ajax_comments($nid, $page, $size){
	$sql = "select * from ".$GLOBALS['Base']->table('company_new_comment')." where new_id=$nid and pid=0 ";
	$sql .= "order by id desc limit ".($page-1)*$size.", $size";
	$res = $GLOBALS['Db']->query($sql);
    //读取管理员回复
    foreach ($res as $k => $v) {
        $res[$k]["child"] = $GLOBALS['Db']->query("select * from ".$GLOBALS['Base']->table('company_new_comment')." where pid=".$v['id']."");
    }
	//print_r($res);
    return $res;
}

function get_article_list($cat_type, $page, $size){
    $sql = "SELECT news.id,news.c_id,news.title,news.content,news.create_time,cat_admin.name cname ".
	       "FROM " . $GLOBALS['Base']->table('company_news') . " as news " .
           "left join " . $GLOBALS['Base']->table('company_new_cat') . ' as cat on cat.id=' . $cat_type . " " .
           "left join " . $GLOBALS['Base']->table('company_admin_new_cat') . ' as cat_admin on cat_admin.id=cat.pid' . " " .
           "WHERE news.cat_type=" . $cat_type . " and news.new_type=0 ".
           "order by news.sort asc, news.id asc limit " . ($page - 1) * $size . "," . $size;
    //详情
    $res = $GLOBALS['Db']->query($sql);
	//print_r($res);
    return $res;
}

?>